
#include "commion.hpp"

int main()
{
    // 1.创建共享空间
    key_t k = ftok(KEY_ADDR, KEY_PRJ_ID );
    if (k == -1)
    {
        perror("fotk fail");
        exit(-1);
    }

    int shmid = shmget(k, SHM_SIZE, 0);
    cout << shmid << endl;
    if (shmid == -1)
    {
        perror("shmget fail");
        exit(-1);
    }
    Log("共享空间加入成功", Test) << endl; 
    
    // 2. 映射虚拟空间，并挂起
    char* shmaddr = (char*)shmat(shmid, nullptr, SHM_W | SHM_R);
    int fd = FifoOpen(FIFO_NAME, WR);

    // 3. 操作
    int time = 10;
    while (time--)
    {
        snprintf(shmaddr, SHM_SIZE - 1, "隔壁的pro哥,又来捞薯薯了!?! [%d]\n", getpid());
        awaking(fd);
        sleep(1);
    }

    strcpy(shmaddr, "quit");
    
    // 4. 去关联
    shmdt(shmaddr);
    Log("删除关联", debug) << endl;
    return 0;
}